﻿package Naruto.Frames {	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.text.TextFormat;
	import flash.text.TextField;
	
	import Naruto.Bloods.*;
	import Naruto.*;

		
	public class Frame0 extends Frame{
		private var _picNum:int;	//加载第x张图片
		private var _container:Sprite;	//按钮组的容器
		private var _btPlay:BtPlay;		//Play按钮
		private var _btCredits:BtCredits;	//Credits按钮
		
		private var _mission1:Mission1;	//任务按钮
		
		private var _btPlayName:String;	//按钮名称
		private var _btCreditsName:String;
		private var _mission1Name:String;
		
		private var _textField:TextField;	//文本
		private var _textFormat:TextFormat;	//文本格式
		private var _textNum:int = 0;		//记录按了几次鼠标。按一次对话翻一篇
		private var _textContent:Array = new Array(new Array("卡卡西：鸣人，这是你的第一个任务，护送达兹纳先生安全到达波之国。\n难度：C"),
										 new Array("达兹纳：鸣人，你在前面走，我会跟着你的。"));
		
		public function Frame0(aStatus:Status, stage:Stage, appStart:Start){
			this._appStart = appStart;
			super(aStatus, stage, appStart);
			init();
		}

		override protected function pubInit():void{			
			keycode = [0];
			
//			this._stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			//this._stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this._stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardEvent);
			this._stage.addEventListener(KeyboardEvent.KEY_UP,onKeyboardEvent);
			
		}
		
		private function init():void{
			do{
				this._picNum = Math.round(10 * Math.random());
			}while(this._picNum>=3)
			
			//this._picNum = 2;
			
			//1.加载背景图片
			this._loadPic = new LoadPic(this._stage,100 + this._picNum);	//加载开始画面
			
			//2.加载按钮			
			addButton(this._picNum);
		}
		private function addButton(picNum:int){
			this._container = new Sprite;
			
			this._btPlay = new BtPlay();
			this._btCredits = new BtCredits();
			this._btPlayName = this._btPlay.name;
			this._btCreditsName = this._btCredits.name;
			
			switch(picNum){
				case 0:{this._container.x = 200;
						this._container.y = 520;
						break;}
				case 1:{this._container.x = 210;
						this._container.y = 420;
						break;}
				case 2:{this._container.x = 650;
						this._container.y = 420;
						break;}
				default:break;
			}
			this._container.addChildAt(this._btPlay,0);
			this._container.addChildAt(this._btCredits,0);
			
			this._btCredits.y = 60;
			
			this._stage.addChild(this._container);
			this._container.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);//给按钮容器加监听（背景无触发）
		}
		
		override public function onMouseDown(event:MouseEvent):void{
			//trace(event.target.name);
			switch(event.target.name){
				case this._btPlayName:{enterPlay();break;}
				case this._btCreditsName:{showCredits();this._stage.addEventListener(MouseEvent.MOUSE_DOWN, clickButton);break;}
				case this._mission1Name:{showText();break;}
				default:break;
			}
		}
		
		//点“开始”
		private function enterPlay(){
			this._container.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			
			var len:uint = this._stage.numChildren;
			while(len>0) {
				this._stage.removeChildAt(0);
				len--;
			}
			this._loadPic = new LoadPic(this._stage,200);	//加载地图
			this._mission1 = new Mission1();
			this._mission1Name = this._mission1.name;
			this._mission1.x = 380;
			this._mission1.y = 480;
			this._stage.addChild(this._mission1);
			this._mission1.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		}
		//点“制作人员”
		private function showCredits(){
			this._container.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			
			var len:uint = this._stage.numChildren;
			while(len>0) {
				this._stage.removeChildAt(0);
				len--;
			}
			this._loadPic = new LoadPic(this._stage,400);	//加载制作人员画面
		}
		private function clickButton(event:MouseEvent){
			var result = this._loadPic.checkButtonName(event.target.name);
			//trace(result);
			switch(result){
				case "Menu":{
								this._stage.removeEventListener(MouseEvent.MOUSE_DOWN, clickButton);
								this._stage.removeChildAt(1);
								this._stage.removeChildAt(0);
								this._appStart._changeFrame(-1);
								break;}
				default:trace("No name");break;
			}
		}
		
		private function showText(){	//创建任务对话
			this._mission1.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
		
			this._textFormat = new TextFormat();
			this._textFormat.color = 0xFFFFFF;
			this._textFormat.font = "隶书";
			this._textFormat.size = 24;
			
			//对话翻篇
			this._stage.addEventListener(MouseEvent.MOUSE_DOWN,nextField);
		}
		private function nextField(event:MouseEvent):void{
			//对话完毕，开始战斗！
			if(this._textNum >= 2){
				this._stage.removeEventListener(MouseEvent.MOUSE_DOWN,nextField);
				clearAll();
			}else{
				//清除上次对话和头像
				if(this._textField){
					if(this._stage.contains(this._textField)){
						this._stage.removeChild(this._textField);
					}
				}
				
				switch(this._textNum){
					case 0:{var headNum = 300;break;}	//加载卡卡西头像
					case 1:{var headNum = 301;this._stage.removeChildAt(2);break;}	//清除上个头像，并加载达兹纳头像
					default:break;
				}				
				this._loadPic.startLoadPic(headNum);	
				
				this._textField = new TextField();
				this._textField.x = 130;
				this._textField.y = 680;
				this._textField.width = 1000;
				
				this._textField.text = this._textContent[this._textNum];
				
				this._textField.setTextFormat(this._textFormat);			
				this._stage.addChild(this._textField);
				
				this._textNum++;
			}
		}
		
		private function clearAll(){
			this._stage.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			this._stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyboardEvent);
			this._stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyboardEvent);
			
			var len:uint = this._stage.numChildren;
			while(len>0) {
				this._stage.removeChildAt(0);
				len--;
			}
			changeFrame();
		}
		override public function changeFrame(changeNum:int = 99){
			this._appStart._changeFrame(0);		//使本幕架空，一切结束
		}

		override public function onEnterFrame(event:Event):void{			
			/*
			//键盘
			for(var k=0;k<keycode.length;k++){
				switch(keycode[k]){
				}				
			}
			*/
		}
	}
}